package com.dbuil.bleshoes;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dbuil.bleshoes.base.BaseActivity;
import com.dbuil.bleshoes.push.TestPushActivity;
import com.dbuil.bleshoes.ui.activity.CountAnimActivity;
import com.dbuil.bleshoes.ui.activity.DialogActivity;
import com.dbuil.bleshoes.ui.activity.GpsActivity;
import com.dbuil.bleshoes.ui.activity.HomeActivity2;
import com.dbuil.bleshoes.ui.activity.MapActivity;
import com.dbuil.bleshoes.ui.activity.SelectTargetActivity;
import com.dbuil.bleshoes.ui.activity.ShareTestActivity;
import com.dbuil.bleshoes.ui.activity.GouWuCarActivity;
import com.dbuil.bleshoes.ui.activity.WhiteSetActivity;
import com.dbuil.bleshoes.ui.activity.WuliuActivity;
import com.dbuil.bleshoes.utils.PrivacyUtils;
import com.xuexiang.xpush.core.XPushManager;
import com.xuexiang.xpush.core.queue.IMessageFilter;
import com.xuexiang.xpush.core.queue.impl.MessageSubscriber;
import com.xuexiang.xpush.entity.CustomMessage;
import com.xuexiang.xpush.entity.Notification;
import com.xuexiang.xpush.notify.NotificationUtils;
import com.xuexiang.xpush.notify.PendingIntentUtils;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;


import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {


    @BindView(R.id.target)
    Button target;
    @BindView(R.id.share)
    Button share;
    @BindView(R.id.whitename)
    Button whitename;
    @BindView(R.id.shopcar)
    Button shopcar;
    @BindView(R.id.gohome)
    Button gohome;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (!NotificationUtils.isNotifyPermissionOpen(this)) {
            new AlertDialog.Builder(this)
                    .setCancelable(false)
                    .setMessage("通知权限未打开，是否前去打开？")
                    .setPositiveButton("是", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface d, int w) {
                            NotificationUtils.openNotifyPermissionSetting(MainActivity.this);
                        }
                    })
                    .setNegativeButton("否", null)
                    .show();
        }
    }

    @Override
    public void initData() {
        super.initData();

        XPushManager.get()
                .addFilter(mMessageFilter)
                .register(mMessageSubscriber);
    }

    private IMessageFilter mMessageFilter = new IMessageFilter() {
        @Override
        public boolean filter(Notification notification) {
            if (StringUtils.isEmpty(notification.getContent()) || notification.getContent().contains("XPush")) {
                ToastUtils.showShort("通知被拦截");
                return true;
            }
            return false;
        }

        @Override
        public boolean filter(CustomMessage message) {
            if (StringUtils.isEmpty(message.getMsg()) || message.getMsg().contains("XPush")) {
                ToastUtils.showShort("自定义消息被拦截");
                return true;
            }
            return false;
        }
    };

    private MessageSubscriber mMessageSubscriber = new MessageSubscriber() {
        @Override
        public void onMessageReceived(CustomMessage message) {
            //  ToastUtils.showShort(String.format("收到自定义消息:%s", message));
            LogUtils.i(String.format("收到自定义消息:%s", message));
        }

        @Override
        public void onNotification(Notification notification) {
            //  ToastUtils.showShort(String.format("收到通知:%s", notification));
            LogUtils.i(String.format("收到通知:%s", notification));
        }

        @Override
        public void onNotificationClick(Notification notification) {
            super.onNotificationClick(notification);
            // ToastUtils.showShort("消息被点击了");
            LogUtils.i("消息被点击了");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ActivityUtils.startActivity(TestPushActivity.class);
                }
            });
        }
    };


    public static final String KEY_PARAM_STRING = "key_param_string";
    public static final String KEY_PARAM_INT = "key_param_int";

    @OnClick({R.id.btn_countanim, R.id.btn_wuliu, R.id.btn_private, R.id.btn_notify, R.id.target, R.id.share, R.id.whitename, R.id.shopcar, R.id.gohome, R.id.dialog, R.id.gps, R.id.guiji})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_countanim://数字动画
                ActivityUtils.startActivity(CountAnimActivity.class);
                break;
            case R.id.btn_wuliu://pdf预览
                ActivityUtils.startActivity(WuliuActivity.class);
                break;
            case R.id.btn_private://隐私权限
                PrivacyUtils.showPrivacyDialog(this, new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

                        dialog.dismiss();
                    }
                });
                break;
            case R.id.btn_notify:
                Map<String, Object> params1 = new HashMap<>();
                params1.put(KEY_PARAM_STRING, "我是参数1");
                params1.put(KEY_PARAM_INT, 123);
                NotificationUtils.buildSimple(1000, R.mipmap.ic_launcher, "告警通知", "测试设备1卫生间电压过压报警提示测试设备1卫生间电压过压报警提示", null)
                        .setContentIntent(PendingIntentUtils.buildActivityIntent(TestPushActivity.class, params1))
                        .setHeadUp(true)
                        // .addAction(R.mipmap.ic_launcher, "确定", PendingIntentUtils.buildBroadcastIntent(NotifyBroadCastReceiver.class, NotifyBroadCastReceiver.ACTION_SUBMIT, 0))
                        //.addAction(R.mipmap.ic_launcher, "取消", PendingIntentUtils.buildBroadcastIntent(NotifyBroadCastReceiver.class, NotifyBroadCastReceiver.ACTION_CANCEL, 0))
                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                        .setIsPolling(false)
                        .show();
                break;
            case R.id.guiji:
                ActivityUtils.startActivity(MapActivity.class);
                break;
            case R.id.gps:
                ActivityUtils.startActivity(GpsActivity.class);
                break;
            case R.id.target:
                ActivityUtils.startActivity(SelectTargetActivity.class);
                break;
            case R.id.share:
                ActivityUtils.startActivity(ShareTestActivity.class);
                break;
            case R.id.whitename:
                ActivityUtils.startActivity(WhiteSetActivity.class);
                break;
            case R.id.shopcar:
                ActivityUtils.startActivity(GouWuCarActivity.class);
                break;
            case R.id.gohome:
                ActivityUtils.startActivity(HomeActivity2.class);
                break;
            case R.id.dialog:
                ActivityUtils.startActivity(DialogActivity.class);
                break;
        }
    }


}
